package ru.spies.dbfunctional;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.spies.dbwork.DBWork;
import ru.spies.entities.Siteuser;
import ru.spies.pages.ErrorPage;
import ru.spies.pages.GoodPage;

/**
 * Servlet implementation class DeleteUser
 */
@WebServlet("/DeleteUser")
public class DeleteUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteUser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String userName=request.getParameter("oldName");
		Siteuser user = new Siteuser();
		user.setParamForFindQuery(userName);
		user = (Siteuser) DBWork.getInstance().findObjectFromTable(user);
		if (user==null){
			out.print(ErrorPage.getInstance().getThisPage());
			return;
		}
		DBWork.getInstance().removeObjectFromTable(user);
		out.print(GoodPage.getInstance().getThisPage());
	}

}
